classdef TestDotsTheScreen < dotsTestCase
    
    properties
        theScreen;
    end
    
    methods
        function self = TestDotsTheScreen(name)
            self = self@dotsTestCase(name);
        end
        
        function setUp(self)
            self.setUp@dotsTestCase;
            
            dotsTheScreen.reset;
            self.theScreen = dotsTheScreen.theObject;
        end
        
        function tearDown(self)
            self.tearDown@dotsTestCase;
            dotsTheScreen.reset;
        end
        
        function testBlankAccounting(self)
            self.theScreen.open;
            
            % blank several times
            n = 10;
            onset = zeros(1,n);
            number = zeros(1,n);
            swap = zeros(1,n);
            for ii = 1:n
                [onset(ii), number(ii), swap(ii)] = ...
                    self.theScreen.blank();
            end
            
            % this is a weak test, to be revisited
            assertTrue(onset(1) < onset(n), ...
                'blank onsets should be later and later')
            assertTrue(number(1) < number(n), ...
                'blank frame numbers should be increasing')
            assertTrue(swap(1) < swap(n), ...
                'blank swaps should be later and later')
            
            self.theScreen.close;
        end
        
        function testFrameAccounting(self)
            self.theScreen.open;
            
            % swap buffers several times
            n = 10;
            onset = zeros(1,n);
            number = zeros(1,n);
            swap = zeros(1,n);
            for ii = 1:n
                [onset(ii), number(ii), swap(ii)] = ...
                    self.theScreen.blank();
            end
            
            % this is a weak test, to be revisited
            assertTrue(onset(1) < onset(n), ...
                'flush onsets should be later and later')
            assertTrue(number(1) < number(n), ...
                'flush frame numbers should be increasing')
            assertTrue(swap(1) < swap(n), ...
                'flush swaps should be later and later')
            
            self.theScreen.close;
        end
        
        function testRedundantOpenClose(self)
            self.theScreen.open;
            self.theScreen.open;
            self.theScreen.close;
            self.theScreen.open;
            self.theScreen.close;
            self.theScreen.close;
        end
        
        function testResetShouldCloseWindow(self)
            self.theScreen.open;
            dotsTheScreen.reset;
            assertTrue(self.theScreen.getDisplayNumber() < 0, ...
                'the screen should know its window has closed')
        end
    end
end